# Copyright 2024 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Remove legacy Artifacts table

Revision ID: 6e8e4df16a4e
Revises: a6f358254cbd
Create Date: 2025-07-21 09:33:01.444888

"""

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "6e8e4df16a4e"
down_revision = "a6f358254cbd"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index("_artifacts_labels_uc", table_name="artifacts_labels")
    op.drop_index("idx_artifacts_labels_name_value", table_name="artifacts_labels")
    op.drop_table("artifacts_labels")
    op.drop_index("_artifacts_tags_uc", table_name="artifacts_tags")
    op.drop_table("artifacts_tags")
    op.drop_index("_artifacts_uc", table_name="artifacts")
    op.drop_table("artifacts")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "artifacts",
        sa.Column("id", mysql.INTEGER(), autoincrement=True, nullable=False),
        sa.Column(
            "key",
            mysql.VARCHAR(charset="utf8mb3", collation="utf8mb3_bin", length=255),
            nullable=True,
        ),
        sa.Column(
            "project",
            mysql.VARCHAR(charset="utf8mb3", collation="utf8mb3_bin", length=255),
            nullable=True,
        ),
        sa.Column(
            "uid",
            mysql.VARCHAR(charset="utf8mb3", collation="utf8mb3_bin", length=255),
            nullable=True,
        ),
        sa.Column("updated", mysql.TIMESTAMP(fsp=3), nullable=True),
        sa.Column("body", mysql.MEDIUMBLOB(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        mysql_collate="utf8mb3_bin",
        mysql_default_charset="utf8mb3",
        mysql_engine="InnoDB",
    )
    op.create_index(
        "_artifacts_uc", "artifacts", ["uid", "project", "key"], unique=True
    )
    op.create_table(
        "artifacts_tags",
        sa.Column("id", mysql.INTEGER(), autoincrement=True, nullable=False),
        sa.Column(
            "project",
            mysql.VARCHAR(charset="utf8mb3", collation="utf8mb3_bin", length=255),
            nullable=True,
        ),
        sa.Column(
            "name",
            mysql.VARCHAR(charset="utf8mb3", collation="utf8mb3_bin", length=255),
            nullable=True,
        ),
        sa.Column("obj_id", mysql.INTEGER(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ["obj_id"],
            ["artifacts.id"],
            name="_artifacts_tags_obj_id_fk",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
        mysql_collate="utf8mb3_bin",
        mysql_default_charset="utf8mb3",
        mysql_engine="InnoDB",
    )
    op.create_index(
        "_artifacts_tags_uc",
        "artifacts_tags",
        ["project", "name", "obj_id"],
        unique=True,
    )
    op.create_table(
        "artifacts_labels",
        sa.Column("id", mysql.INTEGER(), autoincrement=True, nullable=False),
        sa.Column(
            "name",
            mysql.VARCHAR(charset="utf8mb3", collation="utf8mb3_bin", length=255),
            nullable=True,
        ),
        sa.Column(
            "value",
            mysql.VARCHAR(charset="utf8mb3", collation="utf8mb3_bin", length=255),
            nullable=True,
        ),
        sa.Column("parent", mysql.INTEGER(), autoincrement=False, nullable=True),
        sa.ForeignKeyConstraint(
            ["parent"],
            ["artifacts.id"],
            name="_artifacts_labels_paren_fk",
            ondelete="CASCADE",
        ),
        sa.PrimaryKeyConstraint("id"),
        mysql_collate="utf8mb3_bin",
        mysql_default_charset="utf8mb3",
        mysql_engine="InnoDB",
    )
    op.create_index(
        "idx_artifacts_labels_name_value",
        "artifacts_labels",
        ["name", "value"],
        unique=False,
    )
    op.create_index(
        "_artifacts_labels_uc", "artifacts_labels", ["name", "parent"], unique=True
    )
    # ### end Alembic commands ###
